{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from simpletransformers.classification import ClassificationModel, ClassificationArgs\n",
    "from sklearn.metrics import f1_score, precision_score, recall_score, roc_auc_score, accuracy_score\n",
    "import torch\n",
    "\n",
    "cuda_available = torch.cuda.is_available()\n",
    "cuda_available"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#load model fine-tuned on ONQ3\n",
    "model = ClassificationModel(\n",
    "    \"bert\",\n",
    "    r\"02_output data\\models\\onq3m\",\n",
    "    use_cuda=cuda_available\n",
    ")"
   ],
   "id": "8ecb02f3701a548a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#import bot and facebook responses to ONQ1 (\"adoption probe\" question)\n",
    "df_onq1 = pd.read_excel(r\"02_output data\\onq1_balanced_sample.xlsx\")\n",
    "\n",
    "#print df characteristics\n",
    "print(df_onq1.head(5))\n",
    "print(df_onq1.shape)"
   ],
   "id": "43855df8b23b8391",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#make cross-corpus predictions on responses to ONQ1\n",
    "onq1_predictions, _ = model.predict(df_onq1['text'].tolist())\n",
    "\n",
    "#calculate precision, recall, and F1 score\n",
    "onq1_precision = precision_score(df_onq1['label'], onq1_predictions)\n",
    "onq1_recall = recall_score(df_onq1['label'], onq1_predictions)\n",
    "onq1_f1 = f1_score(df_onq1['label'], onq1_predictions)\n",
    "\n",
    "print(f\"Precision: {onq1_precision}\")\n",
    "print(f\"Recall: {onq1_recall}\")\n",
    "print(f\"F1 Score: {onq1_f1}\")"
   ],
   "id": "6c78d90488c535da",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#export df with predictions as excel file\n",
    "results_onq1 = pd.DataFrame({\n",
    "    \"Text\": df_onq1['text'].tolist(),\n",
    "    \"ID\": df_onq1['ID'].tolist(),\n",
    "    \"True Label\": df_onq1['label'].tolist(),\n",
    "    \"Predicted Label\": onq1_predictions\n",
    "})\n",
    "\n",
    "results_onq1.to_excel(r\"02_output data\\onq1_onq3m.xlsx\", index=False, engine=\"openpyxl\")"
   ],
   "id": "ce6e51caf60bec91",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#import bot and facebook responses to ONQ2 (\"discrimination\" question)\n",
    "df_onq2 = pd.read_excel(r\"02_output data\\onq2_balanced_sample.xlsx\")\n",
    "\n",
    "#print df characteristics\n",
    "print(df_onq2.head(5))\n",
    "print(df_onq2.shape)"
   ],
   "id": "cb36ce3370388498",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#make cross-corpus predictions on responses to ONQ2\n",
    "onq2_predictions, _ = model.predict(df_onq2['text'].tolist())\n",
    "\n",
    "#calculate precision, recall, and F1 score\n",
    "onq2_precision = precision_score(df_onq2['label'], onq2_predictions)\n",
    "onq2_recall = recall_score(df_onq2['label'], onq2_predictions)\n",
    "onq2_f1 = f1_score(df_onq2['label'], onq2_predictions)\n",
    "\n",
    "print(f\"Precision: {onq2_precision}\")\n",
    "print(f\"Recall: {onq2_recall}\")\n",
    "print(f\"F1 Score: {onq2_f1}\")"
   ],
   "id": "49ecadec911f0023",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#export df with predictions as excel file\n",
    "results_onq2 = pd.DataFrame({\n",
    "    \"Text\": df_onq2['text'].tolist(),\n",
    "    \"ID\": df_onq2['ID'].tolist(),\n",
    "    \"True Label\": df_onq2['label'].tolist(),\n",
    "    \"Predicted Label\": onq2_predictions\n",
    "})\n",
    "\n",
    "results_onq2.to_excel(r\"02_output data\\onq2_onq3m.xlsx\", index=False, engine=\"openpyxl\")"
   ],
   "id": "1bd395fa99a5683a",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
